package com.djj.nanny.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.djj.nanny.common.Result;
import com.djj.nanny.enumClass.FileType;
import com.djj.nanny.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Tag(name = "文件上传接口类")
@RequestMapping("/file")
@RestController
public class FileController {

    @Autowired
    private FileService fileService;

    @Operation(summary = "文件上传")
    @PostMapping("/upload/img")
    public Result<?> upLoad(@RequestParam("file") MultipartFile file) {
      return fileService.loadFile(file, FileType.IMG);
    }
    @Operation(summary = "多文件上传，并且在有失败时删除已上传成功的文件")
    @PostMapping("/uploadMultiple")
    public Result<?> uploadMultiple(@RequestParam("files") List<MultipartFile> files) {
        List<String> strings = fileService.uploadMultiple(files);
        return !strings.isEmpty() ? Result.success(strings):Result.error("-1","文件上传失败！");
    }
    @DeleteMapping("/remove/{filename}")
    public Result<?> fileRemove(@PathVariable String filename){
        return fileService.deleteFile(filename);
    }
}





